
import { getEncryptedParam } from './util'
import { getCookie } from '@/utils/cookies'
import { CateType, CateConfig, FilesType, AddFile, RecordType, ReqTokenParam, ResGetToken, GetRecordType } from '@/types/apiResType'
import qs from 'qs'
export const hostName = '818ps.com';
export function ipsApi(apiPath: string) { return '//' + hostName + apiPath; }
//|'omit' | 'same-origin'
const options: { credentials: 'include' } = {
    credentials: 'include'
};
export interface BaseResponse<T = any> {
    code: number
    msg: string
    data: T
}

/**获取aiEditor业务 */
//获取分类
export const getAiCate = (): Promise<BaseResponse<CateType>> => {
    return fetch('/apiv5/cate', options).then(res => res.json());
}
//获取分类配置
export const getAiCateConfig = (param: { cateid: string }): Promise<BaseResponse<CateConfig>> => {
    return fetch('/apiv5/cate-config?' + qs.stringify(param), options).then(res => res.json());
}
export const getAiFiles = (param: { page: string }): Promise<BaseResponse<FilesType>> => {
    return fetch('/apiv5/files?' + qs.stringify(param), options).then(res => res.json());
}
export const addAiFile = (param: { name: string }): Promise<BaseResponse<AddFile>> => {
    return fetch(`/apiv5/add-file`, {
        headers:{  'Content-Type': 'application/x-www-form-urlencoded', },
        method: 'POST',
        body: qs.stringify({ ...param }),
        ...options
    }).then(res => res.json());
}
export const deleteFiles = (param: { fileid: string }): Promise<BaseResponse<Array<string>>> => {
    return fetch('/apiv5/del-file?' + qs.stringify(param), options).then(res => res.json());
}
export const postAiRecord = (fileid: string, param: { record: string, record_id: string }): Promise<BaseResponse<RecordType>> => {
    return fetch('/apiv5/record?fileid=' + fileid, {
        method: 'POST',
        body: JSON.stringify({ ...param }),
        ...options
    }).then(res => res.json());
}
export const getAiRecord = (param: { fileid: string }): Promise<BaseResponse<GetRecordType>> => {
    return fetch('/apiv5/record?' + qs.stringify(param), options).then(res => res.json());
}
///apiv5/update-file
export const updateAiFile= (param: { fileid: string,name:string }): Promise<BaseResponse<FilesType>> => {
    return fetch('/apiv5/update-file?' + qs.stringify(param), {
        headers:{  'Content-Type': 'application/x-www-form-urlencoded', },
        method: 'POST',
        body: qs.stringify({ ...param }),
        ...options
    }).then(res => res.json());
}
// 获取风格版式
export const getFormatStyle = () => {
    return fetch('/aidraw/style').then(res => res.json());
}

export const createAiTextJob = (params: ReqTokenParam): Promise<BaseResponse<ResGetToken>> => {
    const encryptionObj = getEncryptedParam()
    const headerObj = {
        // 'Cookie': 'IPSSESSION=' + getCookie('IPSSESSION'),
        'Timestamp': encryptionObj.timestamp,
        'Sign': encryptionObj.sign,
        'Nonce': encryptionObj.nonce
    }
    const headers = new Headers(headerObj as any);
    const data = new FormData()
    for (let key in params) {
        data.append(key, params[key as keyof ReqTokenParam])
    }
    return fetch('/apiv5/get-token', {
        headers: headers,
        // mode: 'omit',
        method: 'POST',
        body: data,
        credentials: 'include'
    }).then(res => res.json()).catch(err => {
        return {
            code: 500,
            message: '网络错误'
        }
    })
}

export const getAiText = (data: ResGetToken & Pick<ReqTokenParam, 'num' | 'promt_type' | 'promt'>, cb: (res: Response) => void): Promise<void | Response> => {
    const encryptionObj = getEncryptedParam()
    const headerObj = {
        // 'Cookie': 'IPSSESSION=' + getCookie('IPSSESSION'),
        // 'Timestamp': encryptionObj.timestamp,
        'Accept': 'text/event-stream',
        // 'Sign': encryptionObj.sign,
        // // 'Nonce': encryptionObj.nonce,
        // "Origin": "http://h5.818ps.com"

    }
    const headers = new Headers(headerObj as any);
    return fetch('https://gapi.818ps.com/se?' + qs.stringify(data), { headers: headers}).then(response => {
        cb(response)
    })
}

export const getAiConsumerPoint = () => {
    return fetch('/aidraw/get-num', options).then(res => res.json());
}
// 获取绘图区域
export const getDrawChannels = () => {
    return fetch(`/aidraw/get-channel`, options).then(res => res.json());
}
// 创建绘图区域
export const createDrawChannel = (name: string) => {
    return fetch(`/aidraw/add-channel`, {
        method: 'POST',
        body: JSON.stringify({ name, open: 0 }),
        ...options
    }).then(res => res.json());
}


export default () => { }


